# Maintainer: Levente Polyak <anthraxx[at]archlinux[dot]org>
# Maintainer: Eric Bélanger <eric@archlinux.org>

pkgbase=jasper
pkgname=('jasper' 'jasper-doc')
pkgver=2.0.33
pkgrel=1
pkgdesc='Software-based implementation of the codec specified in the emerging JPEG-2000 Part-1 standard'
url='https://www.ece.uvic.ca/~mdadams/jasper/'
arch=('x86_64')
license=('custom:JasPer2.0')
makedepends=('glibc' 'libjpeg' 'freeglut' 'libxmu' 'glu' 'cmake' 'doxygen')
options=('staticlibs')
source=(${pkgname}-${pkgver}.tar.gz::https://github.com/mdadams/jasper/archive/version-${pkgver}.tar.gz
        jasper-1.900.1-fix-filename-buffer-overflow.patch)
sha512sums=('ff88c4b55d52803786520e4f0db10f7cb7cd7642694c866ea7859bc79e409590469fc2c63afe522c7f8b0c00062c155c0fe3b83b8928e319173a3ec9fea3e09e'
            'b8d798bf75523c5db263783e42c653dd0cb03deee90be32eddf878bb6893cca02abadd94de6a8c737a5b7fe76f7fb245979f010765e6a95fc520b215e3a2a7f0')
b2sums=('29cd29cf1af285f6054a969991674b12c968b42d70970a1a460fe43e25047b56d2b96a614d0723d9def6c09c9c6b1bdb04289bdcd8cf348449271e5c975074db'
        '9b1927a437ed9b32491f07a210600b5a7d10711ec954dadc030a8238b67e9c6d1a8e67956fd7a5515e7060f0a1651fa86763e21d6d20592f8f00e9d3260722a1')

prepare() {
  cd ${pkgbase}-version-${pkgver}
  patch -p1 < "${srcdir}/jasper-1.900.1-fix-filename-buffer-overflow.patch"
  sed -r 's|(CMAKE_SKIP_BUILD_RPATH) FALSE|\1 TRUE|g' -i CMakeLists.txt
}

build() {
  cd ${pkgbase}-version-${pkgver}

  local options=(
    -DCMAKE_INSTALL_PREFIX=/usr
    -DCMAKE_INSTALL_LIBDIR=lib
    -DCMAKE_BUILD_TYPE=Release
    -DJAS_ENABLE_OPENGL=ON
    -DJAS_ENABLE_LIBJPEG=ON
    -DJAS_ENABLE_AUTOMATIC_DEPENDENCIES=OFF
    -DCMAKE_SKIP_RPATH=ON
  )

  echo "Building static lib..."
  cmake \
    -B build-static \
    "${options[@]}" \
    -DJAS_ENABLE_SHARED=OFF
  make -C build-static

  echo "Building shared lib..."
  cmake \
    -B build-shared \
    "${options[@]}" \
    -DJAS_ENABLE_SHARED=ON
  make -C build-shared
}

check() {
  cd ${pkgbase}-version-${pkgver}/build-static
  make -j1 test
}

package_jasper() {
  depends=('glibc' 'libjpeg' 'libjpeg.so')
  optdepends=('jasper-doc: documentation'
              'freeglut: jiv support'
              'glu: jiv support')
  provides=('libjasper.so')
  cd ${pkgbase}-version-${pkgver}
  make -C build-static DESTDIR="${pkgdir}" install
  make -C build-shared DESTDIR="${pkgdir}" install
  install -Dm 644 LICENSE -t "${pkgdir}/usr/share/licenses/${pkgname}"
  rm -r "${pkgdir}/usr/share/doc"
}

package_jasper-doc() {
  pkgdesc+=' (documentation)'
  optdepends=('jasper')
  cd ${pkgbase}-version-${pkgver}
  make -C build-shared DESTDIR="${pkgdir}" install
  install -Dm 644 LICENSE -t "${pkgdir}/usr/share/licenses/${pkgname}"
  rm -rf "${pkgdir}/usr/"{bin,include,lib,share/man}
}

# vim: ts=2 sw=2 et:
